package com.emm.yixun.website.service.marketing;

import java.math.BigDecimal;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.dto.NeedReceiveRefundDto;
import com.emm.yixun.common.model.dto.NoticeEntity;
import com.emm.yixun.common.model.dto.ReserveAreaDto;
import com.emm.yixun.common.model.enums.BusinessFlagEnum;
import com.emm.yixun.common.model.enums.HousingFundTypeEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.customer.service.SignedServiceFacade;
import com.emm.yixun.marketing.service.NeedReceiveRefundServiceFacade;
import com.emm.yixun.system.service.notice.deal.ReserveAreaNoticeFacade;
import com.emm.yixun.website.common.utils.SpringWebContextUtil;

/**
 * 面积补差
 * 
 * @author jzliu
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class ReserveAreaServiceImp implements ReserveAreaService{
	
	protected Logger logger = LoggerFactory.getLogger(ReserveAreaServiceImp.class);
	
	@Autowired
	private ReserveAreaNoticeFacade reserveAreaNoticeFacade;
	
	@Transactional
	@Override
	public Response<NeedReceiveRefundDto> notify(ReserveAreaDto obj) {
		
		Signed signed = getSignedServiceFacadeImpl().findById(obj.getSignedId()).getEntity();
		NeedReceiveRefundDto needReceiveRefundDto=new NeedReceiveRefundDto();
		needReceiveRefundDto.setRelationId(obj.getId()); //保存面积补差ID
		needReceiveRefundDto.setBusinessType("3");
		needReceiveRefundDto.setTotalReceivablesAmount(obj.getMakeBalance());
		needReceiveRefundDto.setCustomerId(signed.getCustomerId());
		needReceiveRefundDto.setRoomId(signed.getRoomId());
		needReceiveRefundDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_23.getNo());
		needReceiveRefundDto.setReceivablesFunds(HousingFundTypeEnum.FUNDTYPE_23.getDes());
		needReceiveRefundDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_15.getNo());
		needReceiveRefundDto.setReceivablesDate(obj.getReceivableDate());
		needReceiveRefundDto.setPaymentMethod(signed.getPaymentMethod());
		needReceiveRefundDto.setStatus("1");
		needReceiveRefundDto.setPostponedStatus("0");
		if(obj.getMakeBalance().compareTo(new BigDecimal("0"))>0){ //正数  增加应收
			needReceiveRefundDto.setDocType("0");
		}else{
			double makebalance = Math.abs(new Double(obj.getMakeBalance().toString()));
			needReceiveRefundDto.setTotalReceivablesAmount(new BigDecimal(makebalance));
			needReceiveRefundDto.setDocType("1");
		}
		needReceiveRefundDto.setMerchantId(obj.getMerchantId());
		needReceiveRefundDto.setUserCode(obj.getUserCode());
		needReceiveRefundDto.setAccessCode(obj.getAccessCode());
		needReceiveRefundDto.setCreateUserId(obj.getCreateUserId());
		needReceiveRefundDto.setDelFlag("0");
		needReceiveRefundDto.setUpdateUserId(obj.getUpdateUserId());
		needReceiveRefundDto.setUpdateTime(new Date());
		Response<NeedReceiveRefundDto> nrrdSaveResponse = getNeedReceiveRefundServiceFacade().save(needReceiveRefundDto);
		
		/**
		 * （面积补差流程审核通过）推送消息到app
		 * @author ny
		 * @param param noticeType 提醒类型, noticeBusinessId 业务主键Id
		 * @return 【${auditStatus}】房号：${roomName}；客户：${customer};
		 */
		NoticeEntity noticeEntity = new NoticeEntity();
		noticeEntity.setNoticeBusinessId(obj.getSignedId());
		noticeEntity.setNoticeType("10");
		getReserveAreaNoticeFacade().createReserveAreaNotice(noticeEntity);
		return nrrdSaveResponse;
	}
	
	private ReserveAreaNoticeFacade getReserveAreaNoticeFacade(){
		return (ReserveAreaNoticeFacade)SpringWebContextUtil.getApplicationContext().getBean("reserveAreaNoticeFacade");
	}
	
	private SignedServiceFacade getSignedServiceFacadeImpl(){
		return (SignedServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("signedServiceFacade");
	}
	
	private NeedReceiveRefundServiceFacade getNeedReceiveRefundServiceFacade(){
		return (NeedReceiveRefundServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("needReceiveRefundServiceFacade");
	}
	
}
